package com.jerry.secondkill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jerry.secondkill.domain.TestData;
import com.jerry.secondkill.rabbitmq.MQSender;
import com.jerry.secondkill.redis.RedisService;
import com.jerry.secondkill.redis.key.TestDataKey;
import com.jerry.secondkill.result.CodeMsg;
import com.jerry.secondkill.result.Result;
import com.jerry.secondkill.service.TestDataService;

@Controller
@RequestMapping("/demo")
public class DemoController {

	@Autowired
	TestDataService testDataService;
	@Autowired
	RedisService redisService;
	@Autowired
	MQSender sender;
	
	@RequestMapping("/")
    @ResponseBody
    public String home() {
        return "Second Kill 实战!";
    }
	
	@RequestMapping("/error")
    @ResponseBody
    public Result<String> error() {
        return Result.error(CodeMsg.SESSION_ERROR);
    }
	
 	//输出成功json
 	@RequestMapping("/testSuccess")
    @ResponseBody
    public Result<String> testSuccess() {
 		return Result.success("hello,imooc");
    }
 	
 	//输出失败json
 	@RequestMapping("/testError")
    @ResponseBody
    public Result<String> testError() {
 		return Result.error(CodeMsg.SERVER_ERROR);
    }
 	
 	
 	
 	
 	
 	//输出页面
 	@RequestMapping("/testThymeleaf")
    public String  testThymeleaf(Model model) {
 		model.addAttribute("name", "Joshua");
 		return "demo";
    }
 	
 	
 	
 	
 	
 	@RequestMapping("/testdb/insert")
    @ResponseBody
    public Result<Boolean> testDbInsert() {
    	testDataService.insert();
        return Result.success(true);
    }
 	
 	@RequestMapping("/testdb/get")
    @ResponseBody
    public Result<TestData> testDbGet() {
    	TestData testData = testDataService.getById(1);
        return Result.success(testData);
    }
    
    @RequestMapping("/testdb/transactional")
    @ResponseBody
    public Result<Boolean> testDbTransactional() {
    	testDataService.insertByTransactional();
        return Result.success(true);
    }
    
    
    
    
    
    
    @RequestMapping("/testRedis/get")
    @ResponseBody
    public Result<TestData> redisGet() {
    	TestData  data  = redisService.get(TestDataKey.getById, ""+1, TestData.class);
        return Result.success(data);
    }
    
    @RequestMapping("/testRedis/set")
    @ResponseBody
    public Result<Boolean> redisSet() {
    	TestData data  = new TestData();
    	data.setId(1);
    	data.setName("1111");
    	redisService.set(TestDataKey.getById, ""+1, data);    //TestDataKey:id1
        return Result.success(true);
    }
    
    
    
    @RequestMapping("/testmq/direct")
	@ResponseBody
	public Result<String> direct() {
		sender.sendDirect("hello,mq direct");
		return Result.success("hello，mq direct");
	}
    
    @RequestMapping("/testmq/topic")
	@ResponseBody
	public Result<String> topic() {
		sender.sendTopic("hello,mq topic");
		return Result.success("hello，mq topic");
	}
    
    @RequestMapping("/testmq/fanout")
	@ResponseBody
	public Result<String> fanout() {
		sender.sendFanout("hello,mq fanout");
		return Result.success("hello，mq fanout");
	}
    
	@RequestMapping("/testmq/header")
	@ResponseBody
	public Result<String> header() {
		sender.sendHeader("hello,mq header");
		return Result.success("hello，mq header");
	}
	
	
	
	
}
